﻿namespace PrismEx.Fixtures.Steps
{
    using FluentAssertions;

    using Microsoft.Practices.Prism.Events;

    using PrismEx.Events;

    using TechTalk.SpecFlow;

    [Binding]
    public class QueuedEventSteps
    {
        private IEventAggregator eventAggregator = new EventAggregator();
        private int delegateCallCount;

        private SubscriptionToken subscriptionToken;

        [Given(@"an event has been subscribed to")]
        public void GivenAnEventHasBeenSubscribedTo()
        {
            subscriptionToken = eventAggregator.Subscribe<FakeQueuedEvent>(e => delegateCallCount++);
        }

        [Given(@"an event has not been subscribed to")]
        public void GivenAnEventHasNotBeenSubscribedTo()
        {
            // nothing needs to happen it's just a step to give context
        }

        [When(@"the event is unsubscribed from")]
        public void WhenTheEventIsUnsubscribedFrom()
        {
            eventAggregator.GetEvent<FakeQueuedEvent>().Unsubscribe(subscriptionToken);
        }

        [When(@"an event has been subscribed to")]
        public void WhenAnEventHasBeenSubscribedTo()
        {
            subscriptionToken = eventAggregator.Subscribe<FakeQueuedEvent>(e => delegateCallCount++);
        }
        [When(@"an event is published")]
        public void WhenAnEventIsPublished()
        {
            eventAggregator.Publish<FakeQueuedEvent>();
        }

        [Then(@"the subscription delegate should have been called (.*) times")]
        public void ThenTheSubscriptionDelegateShouldHaveBeenCalled1Times(int expectedCallCount)
        {
            delegateCallCount.Should().Be(expectedCallCount);
        }

        public class FakeQueuedEvent : QueuedEvent<FakeQueuedEvent>
        {
            public FakeQueuedEvent()
            {
                
            }
        }
    }
}